package com.pomr.service.aop;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.pomr.model.sys.User;
import com.pomr.util.common.JWT;
import com.pomr.util.interceptor.TokenInterceptor;

@Aspect
@Order(1)//设置优先级 日志第一优先级
@Component
public class LogAopService {
	private static Logger log = LoggerFactory.getLogger(LogAopService.class);
	//声明切入点表达式
	//后面的通知直接使用方法名来引用当前的切点表达式；如果是其他类使用，加上包名即可
	@Pointcut("execution(* com.pomr.util.interceptor.TokenInterceptor.preHandle(..))")
	public void  logAop() {
		// TODO Auto-generated constructor stub
	}
	
	/**
     * 前置通知
     * @param joinPoint
     */
    @Before("logAop()") //该标签声明次方法是一个前置通知：在目标方法开始之前执行(用户名	请求url)
    public void beforMethod(JoinPoint joinPoint) throws Exception{
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        HttpServletRequest request = (HttpServletRequest) args.get(0);
//        String authHeader = request.getHeader("Authorization");//获取请求头部信息
        User user = JWT.findUserByToken(request);
        if(user == null){
        	log.info("null \t"+request.getRequestURL().toString());
        }else{
        	log.info(user.getName()+"\t"+request.getRequestURL().toString());
        }
        
    }
}
